//
//  HZWelcomeViewController.swift
//  555wobo
//
//  Created by 黄某某 on 15/12/17.
//  Copyright © 2015年 黄某某. All rights reserved.
//

import UIKit

import SDWebImage

class HZWelcomeViewController: UIViewController {
    
    private var iconViewBottomCom : NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        setIconView()
        HZUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            if error != nil{
                print("用户信息出错")
                return
            }
            self.setIconView()
        })

    }
    
    private func setIconView(){
        if let avatar_large = HZUserAccount.loadUserAccount()?.avatar_large{
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewBottomCom?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                self.welcomeLabel.alpha = 0
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                        self.welcomeLabel.alpha = 1
                        self.welcomeLabel.hidden = false
                 

                    }, completion: { (_) -> Void in
                        print("动画完成")
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
                        
                })

        }
    }
    
    
    private func prepareUI() {
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        bkgImageView.ff_Fill(view)
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        iconViewBottomCom = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        iconViewBottomCom = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
//        view.addConstraint(iconViewBottomCom!)
        welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
        
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
    }
    private lazy var iconView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    private lazy var bkgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var welcomeLabel : UILabel = {
        let lab = UILabel()
        lab.text = "欢迎归来"
        lab.font = UIFont.systemFontOfSize(14)
        lab.hidden = true
        return lab
    }()
    
    

}
